﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using AsPlugInManager;
using AsLib;

namespace NNLInterface
{
    public partial class frmMaterialNote : NNLInterface.frmBaseBasic
    {


        #region 初始化

        string strSQLHead= "  SELECT  * FROM vie_MaterialQty";
        string strTranNo = "";
        string strGroupNo = "";
        string DeptNo = "";
        string resCode = "";
        string strStartTime = "";
        string strEndTime = "";

        DataTable dtMaterial;


        public frmMaterialNote()
        {
            InitializeComponent();
        }

        public frmMaterialNote(IASPArgList argList)
        {
            InitializeComponent();
            this.CmdSearch.Caption = "生成领料数据";
            common.agrs = (ASPArgList)argList;
            this.ImportButtonVisibility = false;
            this.PrintButtonVisibility = true;
            this.lokDepart.EditValueChanged += new EventHandler(lokDepart_EditValueChanged);
            this.lokDepart.ProcessNewValue += new DevExpress.XtraEditors.Controls.ProcessNewValueEventHandler(lokDepart_ProcessNewValue);

           
        }



        private void frmMaterialNote_Load(object sender, EventArgs e)
        {
            //如果当前工程下的用户名为空，则提示输入
            if (common._userid == "")
            {
                frmLogon frmLog = new frmLogon();
                frmLog.ShowDialog();

                if (frmLog.DialogResult == DialogResult.OK)
                {
                    frmLog.Dispose();
                    frmLog = null;
                }
                else
                {
                    this.Close();
                    return;
                }

            }


            IniDataSource();
        }


        private void IniDataSource()
        {
            this.txtUser.Text = common._userid;
            this.txtUserName.Text = common._username;
            this.txtDeptNo.Text = common._DeptNo;
            this.txtDeptName.Text = common._DeptName;

            lokPlanNo.Properties.ShowDropDown = DevExpress.XtraEditors.Controls.ShowDropDown.Never;
            lokDepart.Properties.ShowDropDown = DevExpress.XtraEditors.Controls.ShowDropDown.Never;


            //设定部门的数据源
            SetDeptDataSource();
            string strSQL = strSQLHead + common._userid  + " where 1=2";
            common.SubShowList(accessdb.GetRecord(strSQL).ADODataRst, this.ListGrid, this.ListView, false);
            common.subIniGrid(this.ListView, common.enumGridStyle.ViewStyle);


            //设定版本号
            strSQL = "SELECT DISTINCT o.TranNo FROM Operation" + common._userid + " o ORDER BY o.TranNo desc";
            DataTable dt = accessdb.GetRecord(strSQL).ADODataRst;
            this.lokPlanNo.Properties.DataSource = dt;
            if (dt.Rows.Count >= 1)
            {
                this.lokPlanNo.Text = dt.Rows[0][0].ToString();
                this.lokPlanNo.EditValue = dt.Rows[0][0].ToString();
            }


            common.GetResourceString(this, this.Name );
            common.RestoreFormLayOut(this.Name, this.layOut, this.grpHead);
            common.ReStoreLayOut(this.ListView, this.Name);
            common.ReStoreToolBarLayout(this.Name, this.barmanager);
            this.SetToolVisible(common.ToolVisible.ImportData);
            this.logButtonVisibility = false;

        }
        #endregion


        #region 控件变化事件


        private void SetDeptDataSource()
        {
            //如果是制5课计划，则部门直接是制5课的
            string strSQL = "SELECT DepartNo,DeptName FROM vie_SysUserDepartMent vsudm WHERE vsudm.UserId='" + this.txtUser.Text + "'";
            this.lokDepart.Properties.DataSource = accessdb.GetRecord(strSQL).ADODataRst;

        }


        void lokDepart_ProcessNewValue(object sender, DevExpress.XtraEditors.Controls.ProcessNewValueEventArgs e)
        {
            lookupeditProcess.LookUpEditProcessNewValue(sender, ref e);

        }

        void lokDepart_EditValueChanged(object sender, EventArgs e)
        {
            if (this.lokDepart.Text != "")
                this.txtDepartName.Text = this.lokDepart.GetColumnValue("DeptName").ToString();
            else
                this.txtDepartName.Text = "";
        }

        private void lokDepart_TextChanged(object sender, EventArgs e)
        {
            if (this.lokDepart.Text != "")
                this.txtDepartName.Text = this.lokDepart.GetColumnValue("DeptName").ToString();
            else
                this.txtDepartName.Text = "";
        }
        #endregion

        #region 工具栏按钮事件

        private void frmMaterialNote_ToolButtonClick(object sender, string e)
        {
            try
            {
                switch (e)
                {
                    case "search":
                        if (!CheckData()) return;

                        if (DevExpress.XtraEditors.XtraMessageBox.Show("确定要生成最新的领料数据吗(Y/N)?", common.sysParameter.MessageInf, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.No) return ;

                        Cursor currentCursor = Cursor.Current;

                        Cursor.Current = Cursors.WaitCursor;

                        strTranNo = this.lokPlanNo.Text;
                        DeptNo = this.lokDepart.Text;
                        strStartTime = this.dteFrom.Text;
                        strEndTime = this.dteTo.Text;

                        string sql = string .Format(" EXEC pro_UpdateMaterialNote" + common._userid + " \n"
                                   + " 	@Tranno = '{0}', \n"
                                   + " 	@dateFrom = '{1}', \n"
                                   + " 	@dateTo = '{2}', \n"
                                   + " 	@UserId ='{3}',"
                                   + " 	@deptNo ='{4}'",
                                   strTranNo,strStartTime,
                                   strEndTime,this.txtUser.Text, lokDepart.Text);
                        accessdb.RsChange(sql);

                        sql = strSQLHead + common._userid + string.Format(" where tranno='{0}'", strTranNo);
                        this.ListGrid.DataSource = accessdb.GetRecord(sql).ADODataRst;
                        common.subIniGrid(this.ListView, common.enumGridStyle.ViewStyle);
                        common.ReStoreLayOut(this.ListView);

                        Cursor.Current = currentCursor;


                        DevExpress.XtraEditors.XtraMessageBox.Show("领料数据已成功生成!\r\n"
                                                                   + "操作者:" + common._userid + "(" + common._username + ")\r\n"
                                                                   + "计划版本号:" + strTranNo,
                                                                   common.sysParameter.MessageInf, MessageBoxButtons.OK, MessageBoxIcon.Information);

                        break;

                    case "excel":
                        subExportXLS();
                        break;

                    case "print":
                        common.PrintView(this.ListGrid, "领料单数据");
                        break;
                    case "exit":
                        common.SaveFormLayOut(this.Name , this.layOut, this.grpHead);
                        common.SaveLayout(this.ListView, this.Name );
                        this.Dispose();
                        break;
                    default:
                        {
                            break;
                        }
                }


            }
            catch (Exception ex)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show(ex.Message, common.sysParameter.MessageInf, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

        }


        private bool CheckData()
        {
            if (this.lokPlanNo.Text == "")
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("生产计划版本号不能为空，请检查!", common.sysParameter.MessageInf, MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.lokPlanNo.Focus();
                return false;
            }


            if (this.lokDepart.Text == "" ) 
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("最终部门不能为空，请检查!", common.sysParameter.MessageInf, MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.lokDepart.Focus();
                return false;
            }

            if (this.dteFrom.Text == "")
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("计划开始日期不能为空，请检查!", common.sysParameter.MessageInf, MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.dteFrom.Focus();
                return false;
            }


            if (this.dteTo.Text == "")
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("计划截止日期不能为空，请检查!", common.sysParameter.MessageInf, MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.dteTo.Focus();
                return false;
            }

            //2012-02-03如果没有生成此版本每日计划数据，则需要提示
            string strTranNo = this.lokPlanNo.Text;
            string strSQL = "SELECT COUNT(*) FROM OperationDetail" + common._userid + " od WHERE od.TranNo='" + strTranNo + "'";

            if (Convert.ToInt32(accessdb.GetField(strSQL)) < 1)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("不能生成此版本的计划的领料数据，请在计划查询界面进行此版本的计划查询操作,\r\n" + "将工作生产数量按天拆分后，再进行领料数据的生成!", common.sysParameter.MessageInf, MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.dteTo.Focus();
                return false;
            }


            return true;
        }


        private void subExportXLS()
        {
            try
            {
                if (!common.CheckExistsData(this.ListView)) return;
                common.ExportExcel(this.ListView);
                return;
            }
            catch (Exception ex)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show(ex.Message, common.sysParameter.MessageInf, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

        }

        #endregion

        //切换用户
        private void txtChange_Click(object sender, EventArgs e)
        {
            frmLogon frmLog = new frmLogon();
            frmLog.ShowDialog();

            if (frmLog.DialogResult == DialogResult.OK)
            {
                IniDataSource();
            }

            frmLog.Dispose();
            frmLog = null;

        }

        private void frmMaterialNote_FormClosing(object sender, FormClosingEventArgs e)
        {
            serial.SerializeBinary(common.sysParameter, Application.StartupPath + @"\NNLInterface.dat");
            common._its_accessdb = null;
            common.pro = null;
            GC.Collect();
        }


    }
}
